<template>
  <business-basic-layout :menus='menus' :title='title' ref='business-basic-layout'>
    <template v-slot:rightContentRender>
      <right-content top-menu :theme='settings.theme' @remove-mark-timer='removeMarkTimer'
                     @hidden-side-menu='handleHiddenSideMenu' />
    </template>
    <transition name='fade-transform' mode='out-in'>
      <router-view></router-view>
    </transition>
  </business-basic-layout>
</template>

<script>
import BusinessBasicLayout from '@/business-components/basic-layout/Index'
import { SettingDrawer, updateTheme } from '@ant-design-vue/pro-layout'
import { mapState } from 'vuex'
import { asyncRouterMap } from '@/config/router.config.js'
import {
  CONTENT_WIDTH_TYPE
  // , SIDEBAR_TYPE, TOGGLE_MOBILE_TYPE
} from '@/store/mutation-types'

import defaultSettings from '@/config/defaultSettings'
import RightContent from '@/components/GlobalHeader/RightContent'
// import GlobalFooter from '@/components/GlobalFooter'
// import Ads from '@/components/Other/CarbonAds'

export default {
  name: 'BasicLayout',
  components: {
    SettingDrawer,
    RightContent,
    BusinessBasicLayout
    // GlobalFooter,
    // Ads
  },
  data() {
    return {
      // preview.pro.antdv.com only use.
      isProPreviewSite: process.env.VUE_APP_PREVIEW === 'true' && process.env.NODE_ENV !== 'development',
      // end
      isDev: process.env.NODE_ENV === 'development' || process.env.VUE_APP_PREVIEW === 'true',

      // base
      menus: [],
      // 侧栏收起状态
      collapsed: false,
      title: defaultSettings.title,
      settings: {
        // 布局类型
        layout: defaultSettings.layout, // 'sidemenu', 'topmenu'
        // CONTENT_WIDTH_TYPE
        contentWidth: defaultSettings.layout === 'sidemenu' ? CONTENT_WIDTH_TYPE.Fluid : defaultSettings.contentWidth,
        // 主题 'dark' | 'light'
        theme: defaultSettings.navTheme,
        // 主色调
        primaryColor: defaultSettings.primaryColor,
        fixedHeader: defaultSettings.fixedHeader,
        fixSiderbar: defaultSettings.fixSiderbar,
        colorWeak: defaultSettings.colorWeak,

        hideHintAlert: false,
        hideCopyButton: false
      },
      // 媒体查询
      query: {},

      // 是否手机模式
      isMobile: false
    }
  },
  computed: {
    ...mapState({
      // 动态主路由
      mainMenu: state => state.permission.addRouters,
      permissionList: state => (state.user.info && state.user.info.permissionList) ? [...new Set(state.user.info.permissionList.map(item => item.permissionCode))] : [],
      userType: state => state.user.info.userType
    })
  },
  created() {
    // const routes = this.mainMenu.find(item => item.path === '/')
    // this.menus = (routes && routes.children) || []
    // // 处理侧栏收起状态
    // this.$watch('collapsed', () => {
    //   this.$store.commit(SIDEBAR_TYPE, this.collapsed)
    // })
    // this.$watch('isMobile', () => {
    //   this.$store.commit(TOGGLE_MOBILE_TYPE, this.isMobile)
    // })

    const routes = asyncRouterMap.find((item) => item.path === '/')
    // const routes = this.mainMenu.find((item) => item.path === '/')
    if (this.userType === '1') {
      // 管理员
      this.menus = (routes && routes.children) || []
    } else {
      // 普通用户
      this.menus = (routes && routes.children.filter(item => {
        if (!item.meta.permissionCode) {
          return false
        }
        // 是否当前页面所需权限都在权限列表中、不在的过滤掉
        const permissionCodeList = item.meta.permissionCode
        return permissionCodeList.every(permissionCodeItem => this.permissionList.includes(permissionCodeItem))
      })) || []
    }
    // if (process.env.NODE_ENV === 'development') {
    //   // 开发
    //   this.menus = (routes && routes.children) || []
    // } else {
    //   // 生产
    //   this.menus = (routes && routes.children.filter(item => item.name !== 'development')) || []
    // }
  },
  mounted() {
    const userAgent = navigator.userAgent
    if (userAgent.indexOf('Edge') > -1) {
      this.$nextTick(() => {
        this.collapsed = !this.collapsed
        setTimeout(() => {
          this.collapsed = !this.collapsed
        }, 16)
      })
    }

    // first update color
    // TIPS: THEME COLOR HANDLER!! PLEASE CHECK THAT!!
    if (process.env.NODE_ENV !== 'production' || process.env.VUE_APP_PREVIEW === 'true') {
      updateTheme(this.settings.primaryColor)
    }
  },
  methods: {
    handleHiddenSideMenu() {
      this.$refs['business-basic-layout'].clearSession()
      this.$refs['business-basic-layout'].changeSideMenuVisable()
    },
    removeMarkTimer() {
      this.$refs['business-basic-layout'].clearMarksTimer()
    }
    //   i18nRender,
    //   handleMediaQuery (val) {
    //     this.query = val
    //     if (this.isMobile && !val['screen-xs']) {
    //       this.isMobile = false
    //       return
    //     }
    //     if (!this.isMobile && val['screen-xs']) {
    //       this.isMobile = true
    //       this.collapsed = false
    //       this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fluid
    //       // this.settings.fixSiderbar = false
    //     }
    //   },
    //   handleCollapse (val) {
    //     this.collapsed = val
    //   },
    //   handleSettingChange ({ type, value }) {
    //     console.log('type', type, value)
    //     type && (this.settings[type] = value)
    //     switch (type) {
    //       case 'contentWidth':
    //         this.settings[type] = value
    //         break
    //       case 'layout':
    //         if (value === 'sidemenu') {
    //           this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fluid
    //         } else {
    //           this.settings.fixSiderbar = false
    //           this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fixed
    //         }
    //         break
    //     }
    //   }
    // }
  }
}
</script>

<style lang='less'>
@import "./BasicLayout.less";
</style>
